package com.haiertvbic.tvnow.widget;

import java.lang.ref.SoftReference;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Random;
import java.util.Timer;
import java.util.TimerTask;

import android.content.Context;
import android.graphics.Bitmap;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.view.animation.Animation.AnimationListener;
import android.widget.BaseAdapter;
import android.widget.ImageView;

import com.haiertvbic.tvnow.R;
import com.haiertvbic.tvnow.network.EPGWeb;
import com.haiertvbic.tvnow.util.Constant;
import com.haiertvbic.tvnow.util.SLog;

public class MyImage {

	protected static final String TAG = "MyImage";
	public ImageView image;
	BaseAdapter adapter;
	String cid;
	Context context;
	Handler handler = new Handler() {

		@Override
		public void handleMessage(Message msg) {
			// TODO Auto-generated method stub
			super.handleMessage(msg);
			switch (msg.what) {
			case 11:
				final Bitmap bms = (Bitmap) msg.obj;

				final AlphaAnimation animIn = new AlphaAnimation(0, 1);
				animIn.setDuration(1000);

				final AlphaAnimation animOut = new AlphaAnimation(1, 0);
				animOut.setDuration(500);
				animOut.setAnimationListener(new AnimationListener() {
					@Override
					public void onAnimationStart(Animation arg0) {
					}

					@Override
					public void onAnimationRepeat(Animation arg0) {
					}

					@Override
					public void onAnimationEnd(Animation arg0) {
						image.clearAnimation();
						image.setImageBitmap(bms);// 要入的动画
						image.startAnimation(animIn);
						adapter.notifyDataSetChanged();
					}
				});
				if (image != null) {
					image.startAnimation(animOut);
					adapter.notifyDataSetChanged();
				}
				break;

			default:
				break;
			}
		}

	};

	public MyImage(final BaseAdapter adapter, String cid, Context context) {
		image = (ImageView) View.inflate(context, R.layout.layout_item, null);
		this.cid = cid;
		this.context = context;
		this.adapter = adapter;
		getPic();
	}

	// 更换图片的,多了放池子里
	private void getPic() {
		final int ttt = (20 + new Random(new Date().getTime()).nextInt(10)) * 1000;
		SLog.d(TAG, "换图的时间是==" + ttt);
		if (Constant.IMAGE_TT != null) {
			Constant.IMAGE_TT.schedule(new TimerTask() {
				private Bitmap bm2;
				HashMap<String, SoftReference<PIPUrlBean>> imageCache = new HashMap<String, SoftReference<PIPUrlBean>>();

				@Override
				public void run() {
					SLog.d(TAG, "开始执行一个获取截图任务");
					ArrayList<PIPUrlBean> pips = EPGWeb.getChannelPIPXML(
							context, cid, 1);
					PIPUrlBean bm = null;
					if (pips != null && pips.size() > 0) {
						bm = pips.get(0);
					}
					if (bm != null) {
						String url = bm.getPipUrl();
						if (imageCache.containsKey(url)
								&& imageCache.get(url) != null) {
							//如果和上一个图一样就不更新
							bm2 = null;
						} else {
							bm2 = bm.getBm();
							// 得到value的方法
							// Collection c = imageCache.values();
							// System.out.println(c);
							// 删除map中该频道多余的图片
							imageCache.remove(url);
							storageBm(bm2);
//							imageCache.clear();// 有这句话上面的代码都可以省略
							imageCache.put(url, new SoftReference<PIPUrlBean>(
									bm));
						}

					}
					if (bm2 != null) {
						Message msg = Message.obtain();
						msg.what = 11;
						msg.obj = bm2;
						handler.sendMessage(msg);
					}
				}

				private void storageBm(Bitmap bm22) {
					HashMap<String, Bitmap> storage = Constant.STORAGE;
					if (storage.containsKey(cid) && storage.get(cid) != null) {
						storage.remove(cid);
						storage.put(cid, bm22);
					} else {
						storage.put(cid, bm22);
					}
				}
			}, 0, ttt);
		}
	}
}
